﻿//----------------------------------------------------------------------------------------------------
// A NodeInstance is the representation of a node in a level
//----------------------------------------------------------------------------------------------------

package HG.Visual
{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Timer;
	
	public class VisualProgress extends MovieClip
	{
		var m_targetProgress:Number;
		var m_currentProgress:Number;
		
		var m_animTimer:Timer;
		
		//----------------------------------------------------------------------------------------------------
		public function VisualProgress()
		{
			m_animTimer = new Timer(25);
			m_animTimer.addEventListener(TimerEvent.TIMER, UpdateProgress);
			
			this.Content.gotoAndStop(1);
			this.visible = false;
			
			m_currentProgress = 0;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function Reset()
		{
			m_currentProgress = 0;
			
			this.Content.TXT_Value.text = "0%";
			this.Content.gotoAndStop(0);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetValue(val:Number)
		{
			m_targetProgress = val;
			
			m_animTimer.start();
			this.gotoAndStop('show');
			this.visible = true;
		}
		
		//----------------------------------------------------------------------------------------------------
		private function UpdateProgress(evt:TimerEvent)
		{
			var diff:Number = m_targetProgress - m_currentProgress;
			
			if (diff <= 5)
			{
				m_animTimer.stop();
				m_currentProgress = m_targetProgress;
				
				if (m_currentProgress == 100)
				{
					this.gotoAndPlay('out');
				}
			}
			else
			{
				diff /= 12;
				diff = Math.round(diff);
				m_currentProgress += diff;
			}
			
			// Just in case.
			m_currentProgress = Math.round(m_currentProgress);
			
			this.Content.TXT_Value.text = m_currentProgress + "%";
			this.Content.gotoAndStop(m_currentProgress);
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}